package com.microservice.msmservice.controller;


import com.microservice.commonutils.R;
import com.microservice.msmservice.entity.MailVo;
import com.microservice.msmservice.service.MailService;
import com.microservice.msmservice.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("email")
public class EmailController {

    @Autowired
    private MailService mailService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("sendCode/{recipient}")
    public R sendCode(String recipient){
        //1 从redis获取验证码，如果获取到直接返回
        String code = redisTemplate.opsForValue().get(recipient);
        if(!StringUtils.isEmpty(code)) {
            return R.ok().message("已发送");
        }
        //2 如果redis获取 不到，进行阿里云发送
        //生成随机值，传递阿里云进行发送
        code = RandomUtil.getFourBitRandom();
//        Map<String,Object> param = new HashMap<>();
//        param.put("code",code);
        //调用service发送短信的方法
//        boolean isSend = msmService.send(code,phone);
        MailVo mailVo=new MailVo();
        mailVo.setText("您的验证码为"+code+",请在5分钟内使用");
        if (recipient==null){
            return R.error().message("recipient is null");
        }else {
            mailVo.setTo(recipient);
        }
        mailVo.setFrom("lesin007@163.com");

        mailVo.setSubject(" ****验证码****");
        MailVo isSend= mailService.sendMail(mailVo);

        if(isSend!=null) {
            //发送成功，把发送成功验证码放到redis里面
            //设置有效时间
            redisTemplate.opsForValue().set(recipient,code,5, TimeUnit.MINUTES);
            return   R.ok().data("mailVo",isSend).data("code",code);
        } else {
            return R.error().message("短信发送失败");
        }

    }









    @GetMapping("sentEmail")
    public void sendMail(){
        MailVo mailVo=new MailVo();
        String author="lee";
        String mail="1531451260@qq.com";
        String text="this is mail content";

        mailVo.setFrom("2452563437@qq.com");
        mailVo.setTo("1531451260@qq.com");
        mailVo.setSubject("收到您朋友【"+author+"】的信件,Ta的邮箱:"+mail);
        mailVo.setText(text);

        mailService.sendMail(mailVo);

    }

    @GetMapping("sentEmail/{content}/{recipient}/{subject}")
//    @ApiImplicitParams({@ApiImplicitParam(name = "subject",value = "subject" , dataType = "String" ),
//            @ApiImplicitParam(name = "content",value = "content", dataType = "String",required = true),
//          })

    public R sendMailByMessage(@PathVariable String content , @PathVariable String recipient , @PathVariable  String subject ){

        MailVo mailVo = new MailVo();
        if (content=="content"){
            return R.ok().message("content is null");
        }else {
            mailVo.setText(content);
        }
       if (recipient==null){
           return R.error().message("recipient is null");
       }else {
           mailVo.setTo(recipient);
       }
        mailVo.setFrom("lesin007@163.com");

        if (subject=="{subject}"){
            mailVo.setSubject("-----this mail is no subject");
        }else {
            mailVo.setSubject(subject);
        }


        mailService.sendMail(mailVo);
        return R.ok().data("mailVo",mailVo);
    }

    @GetMapping("sendEmailByCode/{recipient}/{code}")
    public R sendEmailByCode(@PathVariable String recipient ,@PathVariable int code){
        MailVo mailVo = new MailVo();
        mailVo.setText("您的验证码为: "+code+" ,请在---秒内使用 ");

        if (recipient==null){
            return R.error().message("recipient is null");
        }else {
            mailVo.setTo(recipient);
        }

        mailVo.setFrom("lesin007@163.com");

        mailVo.setSubject(" ****验证码****");
        mailService.sendMail(mailVo);

      return   R.ok().data("mailVo",mailVo).data("code",code);
    }
    @GetMapping("sendEmail/{MailVo}")
    public R sendEmailByMailVo(@PathVariable MailVo mailVo){
        mailService.sendMail(mailVo);
        return R.ok().data("mailVo",mailVo);
    }

}
